package cn.gdcp.valueanimator;

import android.animation.AnimatorInflater;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "1001";
    private Button btnAnim;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnAnim = findViewById(R.id.btn_anim);
        btnAnim.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startPropertyAnimWithJava();
//                startPropertyAnimWithXml();
            }
        });
    }

    private void startPropertyAnimWithJava() {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(btnAnim.getLayoutParams().width, 500);
        valueAnimator.setDuration(3000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int value = (int) valueAnimator.getAnimatedValue();
                Log.e(TAG, "value= " + value);
                btnAnim.getLayoutParams().width = value;
                btnAnim.requestLayout();
            }
        });
        valueAnimator.setRepeatCount(5);
        valueAnimator.setRepeatMode(valueAnimator.REVERSE);
        valueAnimator.start();
    }

    private void startPropertyAnimWithXml() {
        ValueAnimator valueAnimator = (ValueAnimator) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.set_animatior);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int value = (int) valueAnimator.getAnimatedValue();
                Log.e(TAG, "value= " + value);
                btnAnim.getLayoutParams().width = value;
                btnAnim.getLayoutParams().height = value;
                btnAnim.setText("属性动画" + value);
                btnAnim.setBackgroundColor(Color.argb(255, value / 2, 0, 0));
                btnAnim.requestLayout();
            }
        });
        valueAnimator.start();
    }
}


























